package input;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import object.connector.Bar;
import object.connector.Muscle;
import object.connector.Spring;
import object.mass.FixedMass;
import object.mass.Mass;
import sim.Assembly;
import sim.Simulation;

/**
 * 
 * @author Robert Duvall, modified extensively by Niel Lebeck
 * and Wynne Lok
 *
 */

public class Factory {
    public void loadModel (Simulation sim, File modelFile) {
        Assembly curAssembly = new Assembly(sim);
        try {
            Scanner input = new Scanner(modelFile);
            while (input.hasNext()) {
                String lineString = new String(input.nextLine());
                Scanner line = new Scanner(lineString);
                if (line.hasNext()) {
                    String type = line.next();
                    
                    //figure out whether to create a mass or a spring
                    if (type.equals("mass")) {
                        curAssembly.add(massCommand(lineString));
                    }
                    else if (type.equals("spring") || type.equals("muscle")) {
                        curAssembly.add(springCommand(lineString, curAssembly));
                    }
                }
            }
            input.close();
            sim.addAssembly(curAssembly);
        }
        catch (FileNotFoundException e) {
            // should not happen because File came from user selection
            e.printStackTrace();
        }
    }

    private Mass massCommand (String lineString) {
        Scanner line = new Scanner(lineString);
        String inType = line.next();
        int id = line.nextInt();
        double x = line.nextDouble();
        double y = line.nextDouble();
        double mass = line.nextDouble();
        
        //figure out what type of Mass to return
        String type;
        if (mass < 0.0) {
            type = "fixedMass";
        }
        else {
            type = "mass";
        }
        
        Map<String, Mass> massMap = new HashMap<String, Mass>();
        massMap.put("mass", new Mass(id, x, y, mass));
        massMap.put("fixedMass", new FixedMass(id, x, y, mass));
        
        return massMap.get(type);
    }

    private Spring springCommand (String lineString, Assembly curAssembly) {
        Scanner line = new Scanner(lineString);
        String inType = line.next();
        int m1 = line.nextInt();
        int m2 = line.nextInt();
        double restLength = line.nextDouble();
        double ks = line.nextDouble();
        double amplitude = 0;
        if (line.hasNext()) {
            amplitude = line.nextDouble();
        }
        
        //figure out what kind of spring to return
        String type = inType;
        if (ks < 0.0 && "spring".equals(inType)) {
            type = "bar";
        }
        
        Map<String, Spring> springMap = new HashMap<String, Spring>();
        springMap.put("spring", new Spring(curAssembly.getMass(m1),
                curAssembly.getMass(m2), restLength, ks));
        springMap.put("muscle", new Muscle(curAssembly.getMass(m1),
                curAssembly.getMass(m2), restLength, ks, amplitude));
        springMap.put("bar", new Bar(curAssembly.getMass(m1),
                curAssembly.getMass(m2), restLength, ks));
        
        return springMap.get(type);
    }
}
